package cn.tyoui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tyoui.bean.cfddBean;
import cn.tyoui.service.CfddManageService;

@Controller
public class cfddController {

	@Resource
	private CfddManageService cfddManageService;
	
	@ResponseBody
	@RequestMapping("btn_delete_cfdd")
	public Map<String,Object> deletecfdd(@RequestBody Map<String,String> map){
		Map<String,Object> resultMap = new HashMap<>();
		String id = map.get("id");
		System.out.println("id : " + id);
		boolean isSuccess = false;
		if(id != null && !id.equals("")) {
			try {
				cfddManageService.deleteCfdd(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			isSuccess = true;
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("change_cfdd")
	public Map<String,Object> changecfdd(@RequestBody cfddBean cfdd){
		System.out.println("change_cfdd");
		System.out.println(cfdd);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(cfdd != null && cfdd.getId() != 0 && !cfdd.getCfdd().equals("")) {
			try {
				List<cfddBean> cfdds = cfddManageService.queryCfdd(cfdd);
				System.out.println("cfdd  :  "  + cfdds.size());
				if(cfdds.size() == 0) {
					cfddManageService.changeCfdd(cfdd);
					isSuccess = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("isSuccess", isSuccess);
		resultMap.put("status", "200");
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("query_cfdd")
	public Map<String,Object> querycfdd(@RequestBody cfddBean cfdd){
		System.out.println("query_cfdd");
		System.out.println(cfdd);
		Map<String,Object> resultMap = new HashMap<>();
		if(cfdd != null) {
			try {
				List<cfddBean> cfdds = cfddManageService.queryCfdd(cfdd);
				System.out.println(cfdds.size());
				resultMap.put("cfdds", cfdds);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("add_cfdd")
	public Map<String,Object> addcfdd(@RequestBody cfddBean cfdd){
		System.out.println("add_cfdd");
		System.out.println(cfdd);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		if(cfdd != null) {
			try {
				List<cfddBean> cfdds = cfddManageService.queryCfdd(cfdd);
				System.out.println(cfdds.size());
				if(cfdds.size() == 0) {
					isSuccess = cfddManageService.addCfdd(cfdd);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
}
